package com.rem.designPattern.decorator装饰者;

import java.math.BigDecimal;

/**
 * 装饰者模式
 * 动态地给一个对象添加一些额外的职责。就增加功能来说，Decorator 模式相比生成子类更为灵活
 * <p>
 * 配料类和具体实现
 *
 * @author Rem
 * @date 2022-11-17
 */
public abstract class Garnish extends FastFood {
    private FastFood fastFood;

    public Garnish(FastFood fastFood, String name, BigDecimal price) {
        super(name, price);
        this.fastFood = fastFood;
    }

    public FastFood getFastFood() {
        return fastFood;
    }

    public void setFastFood(FastFood fastFood) {
        this.fastFood = fastFood;
    }
}

class Egg extends Garnish {

    public Egg(FastFood fastFood) {
        super(fastFood, "鸡蛋", new BigDecimal(1));
    }

    @Override
    public BigDecimal cost() {
        //返回上一个点的快餐和这个配料的价格累加
        BigDecimal price = getFastFood().getPrice().add(this.getPrice());
        setPrice(price);
        return price;
    }

    @Override
    public String getName() {
        return super.getName() + getFastFood().getName();
    }
}

class Bacon extends Garnish {

    public Bacon(FastFood fastFood) {
        super(fastFood, "培根", new BigDecimal(2));
    }

    @Override
    public BigDecimal cost() {
        //返回上一个点的快餐和这个配料的价格累加
        BigDecimal price = getFastFood().getPrice().add(this.getPrice());
        setPrice(price);
        return price;
    }

    @Override
    public String getName() {
        return super.getName() + getFastFood().getName();
    }
}